{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.233045Z",
     "start_time": "2025-04-27T11:54:12.229482Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "\n",
    "net = nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 1))\n",
    "X = torch.rand(size=(2, 4))\n",
    "print(net(X))"
   ],
   "id": "3c14c23d8609a53",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.3946],\n",
      "        [0.0119]], grad_fn=<AddmmBackward0>)\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.264175Z",
     "start_time": "2025-04-27T11:54:12.250096Z"
    }
   },
   "cell_type": "code",
   "source": "print(net[2].state_dict()) #从0开始索引，所以是（8,1）那一层",
   "id": "496ce7d98c056861",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OrderedDict([('weight', tensor([[-0.0728, -0.1909,  0.1875,  0.2253, -0.1248, -0.3398,  0.2751, -0.2847]])), ('bias', tensor([0.0911]))])\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.294667Z",
     "start_time": "2025-04-27T11:54:12.280596Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(type(net[2].bias))\n",
    "print(net[2].bias)\n",
    "print(net[2].bias.data)"
   ],
   "id": "5c412c6b265acdab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'torch.nn.parameter.Parameter'>\n",
      "Parameter containing:\n",
      "tensor([0.0911], requires_grad=True)\n",
      "tensor([0.0911])\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.326259Z",
     "start_time": "2025-04-27T11:54:12.312010Z"
    }
   },
   "cell_type": "code",
   "source": "net[2].weight.grad == None",
   "id": "1d810efed86b0fc6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.357597Z",
     "start_time": "2025-04-27T11:54:12.343365Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(*[(name, param.shape) for name, param in net[0].named_parameters()])\n",
    "print(*[(name, param.shape) for name, param in net.named_parameters()])"
   ],
   "id": "b5cf326bd53a852",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('weight', torch.Size([8, 4])) ('bias', torch.Size([8]))\n",
      "('0.weight', torch.Size([8, 4])) ('0.bias', torch.Size([8])) ('2.weight', torch.Size([1, 8])) ('2.bias', torch.Size([1]))\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.388384Z",
     "start_time": "2025-04-27T11:54:12.374357Z"
    }
   },
   "cell_type": "code",
   "source": "net.state_dict()['2.bias'].data",
   "id": "6897609f558a3307",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.0911])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.419679Z",
     "start_time": "2025-04-27T11:54:12.405417Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def block1():\n",
    "    return nn.Sequential(nn.Linear(4, 8), nn.ReLU(),\n",
    "                         nn.Linear(8, 4), nn.ReLU())\n",
    "\n",
    "def block2():\n",
    "    net = nn.Sequential()\n",
    "    for i in range(4):\n",
    "        # 在这里嵌套\n",
    "        net.add_module(f'block {i}', block1())\n",
    "    return net\n",
    "\n",
    "rgnet = nn.Sequential(block2(), nn.Linear(4, 1))\n",
    "rgnet(X)"
   ],
   "id": "ecb10d61a590c144",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.2686],\n",
       "        [0.2686]], grad_fn=<AddmmBackward0>)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.496515Z",
     "start_time": "2025-04-27T11:54:12.482161Z"
    }
   },
   "cell_type": "code",
   "source": "print(rgnet)",
   "id": "4731773f036ed5eb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sequential(\n",
      "  (0): Sequential(\n",
      "    (block 0): Sequential(\n",
      "      (0): Linear(in_features=4, out_features=8, bias=True)\n",
      "      (1): ReLU()\n",
      "      (2): Linear(in_features=8, out_features=4, bias=True)\n",
      "      (3): ReLU()\n",
      "    )\n",
      "    (block 1): Sequential(\n",
      "      (0): Linear(in_features=4, out_features=8, bias=True)\n",
      "      (1): ReLU()\n",
      "      (2): Linear(in_features=8, out_features=4, bias=True)\n",
      "      (3): ReLU()\n",
      "    )\n",
      "    (block 2): Sequential(\n",
      "      (0): Linear(in_features=4, out_features=8, bias=True)\n",
      "      (1): ReLU()\n",
      "      (2): Linear(in_features=8, out_features=4, bias=True)\n",
      "      (3): ReLU()\n",
      "    )\n",
      "    (block 3): Sequential(\n",
      "      (0): Linear(in_features=4, out_features=8, bias=True)\n",
      "      (1): ReLU()\n",
      "      (2): Linear(in_features=8, out_features=4, bias=True)\n",
      "      (3): ReLU()\n",
      "    )\n",
      "  )\n",
      "  (1): Linear(in_features=4, out_features=1, bias=True)\n",
      ")\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.573374Z",
     "start_time": "2025-04-27T11:54:12.559200Z"
    }
   },
   "cell_type": "code",
   "source": "rgnet[0][1][0].bias.data",
   "id": "f0dccd516ea563df",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-0.4578,  0.3567,  0.4336, -0.0941, -0.2763, -0.0184,  0.0027, -0.4280])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.619030Z",
     "start_time": "2025-04-27T11:54:12.604259Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def init_normal(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        nn.init.normal_(m.weight, mean=0, std=0.01)\n",
    "        nn.init.zeros_(m.bias)\n",
    "net.apply(init_normal)\n",
    "net[0].weight.data[0], net[0].bias.data[0]"
   ],
   "id": "968e5e70c12b493e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 0.0159, -0.0157, -0.0044, -0.0046]), tensor(0.))"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.650389Z",
     "start_time": "2025-04-27T11:54:12.635678Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def init_constant(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        nn.init.constant_(m.weight, 1)\n",
    "        nn.init.zeros_(m.bias)\n",
    "net.apply(init_constant)\n",
    "net[0].weight.data[0], net[0].bias.data[0]"
   ],
   "id": "c76e8f3428b0f5d0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([1., 1., 1., 1.]), tensor(0.))"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.681900Z",
     "start_time": "2025-04-27T11:54:12.666305Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def init_xavier(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        nn.init.xavier_uniform_(m.weight)\n",
    "def init_42(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        nn.init.constant_(m.weight, 42)\n",
    "\n",
    "net[0].apply(init_xavier)\n",
    "net[2].apply(init_42)\n",
    "print(net[0].weight.data[0])\n",
    "print(net[2].weight.data)"
   ],
   "id": "635955dc8710b2df",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([ 0.2344,  0.2513,  0.6280, -0.3312])\n",
      "tensor([[42., 42., 42., 42., 42., 42., 42., 42.]])\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.713310Z",
     "start_time": "2025-04-27T11:54:12.698223Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def my_init(m):\n",
    "    if type(m) == nn.Linear:\n",
    "        print(\"Init\", *[(name, param.shape)\n",
    "                        for name, param in m.named_parameters()][0])\n",
    "        nn.init.uniform_(m.weight, -10, 10)\n",
    "        m.weight.data *= m.weight.data.abs() >= 5\n",
    "\n",
    "net.apply(my_init)\n",
    "print(net[0].weight[:2])"
   ],
   "id": "bf7c46576a70bfcc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Init weight torch.Size([8, 4])\n",
      "Init weight torch.Size([1, 8])\n",
      "tensor([[-9.6765, -6.9169, -8.2115,  9.1933],\n",
      "        [ 0.0000,  0.0000,  0.0000, -0.0000]], grad_fn=<SliceBackward0>)\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.744364Z",
     "start_time": "2025-04-27T11:54:12.728963Z"
    }
   },
   "cell_type": "code",
   "source": [
    "net[0].weight.data[:] += 1\n",
    "net[0].weight.data[0,0] = 42\n",
    "net[0].weight.data[0]"
   ],
   "id": "91bc9e1291288530",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([42.0000, -5.9169, -7.2115, 10.1933])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.775201Z",
     "start_time": "2025-04-27T11:54:12.761273Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 我们需要给共享层一个名称，以便可以引用它的参数\n",
    "shared = nn.Linear(8, 8)\n",
    "net = nn.Sequential(nn.Linear(4, 8), nn.ReLU(),\n",
    "                    shared, nn.ReLU(),\n",
    "                    shared, nn.ReLU(),\n",
    "                    nn.Linear(8, 1))\n",
    "net(X)\n",
    "# 检查参数是否相同\n",
    "print(net[2].weight.data[0] == net[4].weight.data[0])\n",
    "net[2].weight.data[0, 0] = 100\n",
    "# 确保它们实际上是同一个对象，而不只是有相同的值\n",
    "print(net[2].weight.data[0] == net[4].weight.data[0])"
   ],
   "id": "d7b4154959df35e6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([True, True, True, True, True, True, True, True])\n",
      "tensor([True, True, True, True, True, True, True, True])\n"
     ]
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T11:54:12.806223Z",
     "start_time": "2025-04-27T11:54:12.790865Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "4f8b3ca49d1a567f",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
